import 'package:flutter/material.dart';

class PaintLayer {
  PaintLayer({
    this.color = Colors.black,
    this.stroke = StrokeCap.round,
    this.width = 1.0,
    this.blendMode = BlendMode.screen,
  });

  final Color color;

  final StrokeCap stroke;

  final double width;

  final BlendMode blendMode;

  final _points = <Offset>[];

  bool _shouldPaint = false;

  bool get shouldPaint => _shouldPaint;

  bool get hasPoint => _points.length > 0;

  void clear() {
    _points.clear();
  }

  void addPoint(Offset point) {
    _points.add(point);
    _shouldPaint = true;
  }

  void paint(Canvas canvas) {
    if (_points.length < 1) return;
    var paint = Paint()
      ..color = color
      ..strokeCap = stroke
      ..strokeWidth = width
      ..blendMode = blendMode;
    for (int i = 0; i < _points.length - 1; i++) {
      canvas.drawLine(_points[i], _points[i + 1], paint);
    }
    _shouldPaint = false;
  }
}
